package com.evil.enterprise.controller;

import com.evil.application.api.RemoteApplicationService;
import com.evil.application.pojo.dto.*;
import com.evil.common.core.entity.R;
import com.evil.common.log.annotation.RequestLogAnnotation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 应用接口
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/application")
public class ApplicationController {

    private final RemoteApplicationService remoteApplicationService;

    /**
     * 应用列表
     *
     * @param applicationReqDTO applicationReqDTO
     * @return R
     */
    @PostMapping("/list")
    public R<List<ApplicationInfoRespDTO>> applicationList(@Validated @RequestBody ApplicationReqDTO applicationReqDTO) {
        return R.success(remoteApplicationService.applicationList(applicationReqDTO));
    }

    /**
     * 应用基础信息列表
     *
     * @param applicationReqDTO applicationReqDTO
     * @return R
     */
    @PostMapping("/bases")
    public R<List<ApplicationBaseInfoRespDTO>> applicationBases(@Validated @RequestBody ApplicationReqDTO applicationReqDTO) {
        return R.success(remoteApplicationService.applicationBases(applicationReqDTO));
    }

    /**
     * 应用树
     *
     * @param applicationReqDTO applicationReqDTO
     * @return R
     */
    @PostMapping("/tree")
    public R<List<AppTreeDTO>> applicationTree(@Validated @RequestBody ApplicationReqDTO applicationReqDTO) {
        return R.success(remoteApplicationService.applicationTree(applicationReqDTO));
    }

    /**
     * 应用信息
     *
     * @param applicationKey applicationKey
     * @return R
     */
    @PostMapping("/info")
    public R<ApplicationInfoRespDTO> applicationInfo(@RequestParam("applicationKey") String applicationKey) {
        return R.success(remoteApplicationService.applicationInfo(applicationKey));
    }

    /**
     * 应用基础信息
     *
     * @param applicationKey applicationKey
     * @return R
     */
    @PostMapping("/base-info")
    public R<ApplicationBaseInfoRespDTO> applicationBaseInfo(@RequestParam("applicationKey") String applicationKey) {
        return R.success(remoteApplicationService.applicationBaseInfo(applicationKey));
    }

    /**
     * 应用发布信息
     *
     * @param applicationKey applicationKey
     * @return R
     */
    @PostMapping("/publish-info")
    public R<ApplicationPublishInfoRespDTO> applicationPublishInfo(@RequestParam("applicationKey") String applicationKey) {
        return R.success(remoteApplicationService.applicationPublishInfo(applicationKey));
    }

    /**
     * 新增空白应用
     *
     * @param addApplicationReqDTO addApplicationReqDTO
     * @return R
     */
    @RequestLogAnnotation
    @PostMapping("/add-empty")
    public R<ApplicationBaseInfoRespDTO> addApplication(@Validated @RequestBody AddApplicationReqDTO addApplicationReqDTO) {
        return R.success(remoteApplicationService.addApplication(addApplicationReqDTO));
    }

    /**
     * 编辑应用
     *
     * @param modifyApplicationReqDTO modifyApplicationReqDTO
     * @return R
     */
    @RequestLogAnnotation
    @PostMapping("/modify")
    public R<?> modifyApplication(@Validated @RequestBody ModifyApplicationReqDTO modifyApplicationReqDTO) {
        remoteApplicationService.modifyApplication(modifyApplicationReqDTO);
        return R.success();
    }

    /**
     * 编辑应用权限
     *
     * @param modifyApplicationPermissionReqDTO modifyAppPermissionReqDTO
     * @return R
     */
    @RequestLogAnnotation
    @PostMapping("/modify-permission")
    public R<?> modifyAppPermission(@Validated @RequestBody ModifyApplicationPermissionReqDTO modifyApplicationPermissionReqDTO) {
        remoteApplicationService.modifyAppPermission(modifyApplicationPermissionReqDTO);
        return R.success();
    }

    /**
     * 复制应用
     *
     * @param copyApplicationReqDTO copyApplicationReqDTO
     * @return R
     */
    @RequestLogAnnotation
    @PostMapping("/copy")
    public R<?> copyApplication(@Validated @RequestBody CopyApplicationReqDTO copyApplicationReqDTO) {
        remoteApplicationService.copyApplication(copyApplicationReqDTO);
        return R.success();
    }

    /**
     * 设为常用应用
     *
     * @param favouriteApplicationReqDTO favouriteApplicationReqDTO
     * @return R
     */
    @PostMapping("/favourite")
    public R<?> favouriteApplication(@Validated @RequestBody FavouriteApplicationReqDTO favouriteApplicationReqDTO) {
        remoteApplicationService.favouriteApplication(favouriteApplicationReqDTO);
        return R.success();
    }

    /**
     * 常用应用列表
     *
     * @return R
     */
    @PostMapping("/favourite/list")
    public R<List<ApplicationInfoDTO>> favouriteApplicationList() {
        return R.success(remoteApplicationService.favouriteApplicationList());
    }

    /**
     * 删除应用
     *
     * @param applicationKey applicationKey
     * @return R
     */
    @RequestLogAnnotation
    @PostMapping("/delete")
    public R<?> deleteApplication(@RequestParam("applicationKey") String applicationKey) {
        remoteApplicationService.deleteApplication(applicationKey);
        return R.success();
    }

    /**
     * 发布应用
     *
     * @param applicationKey applicationKey
     * @return R
     */
    @RequestLogAnnotation
    @PostMapping("/publish")
    public R<?> publishApplication(@RequestParam("applicationKey") String applicationKey) {
        remoteApplicationService.publishApplication(applicationKey);
        return R.success();
    }

    /**
     * 还原应用
     *
     * @param applicationKey applicationKey
     * @return R
     */
    @RequestLogAnnotation
    @PostMapping("/restore")
    public R<?> restoreApplication(@RequestParam("applicationKey") String applicationKey) {
        remoteApplicationService.restoreApplication(applicationKey);
        return R.success();
    }

    /**
     * 编辑应用状态
     *
     * @param modifyApplicationStatusReqDTO modifyApplicationStatusReqDTO
     * @return R
     */
    @RequestLogAnnotation
    @PostMapping("/status/modify")
    public R<?> modifyApplicationStatus(@Validated @RequestBody ModifyApplicationStatusReqDTO modifyApplicationStatusReqDTO) {
        remoteApplicationService.modifyApplicationStatus(modifyApplicationStatusReqDTO);
        return R.success();
    }

    /**
     * 编辑应用申请状态
     *
     * @param modifyApplicationApplyReqDTO modifyApplicationApplyReqDTO
     * @return R
     */
    @RequestLogAnnotation
    @PostMapping("/apply/modify")
    public R<?> modifyApplicationApply(@Validated @RequestBody ModifyApplicationApplyReqDTO modifyApplicationApplyReqDTO) {
        remoteApplicationService.modifyApplicationApply(modifyApplicationApplyReqDTO);
        return R.success();
    }
}
